<?php

/**
 * This is the model class for table "content".
 *
 * The followings are the available columns in table 'content':
 * @property integer $id
 * @property integer $content_categoryId
 * @property string $title
 * @property string $desc
 * @property string $image
 * @property string $created_by
 * @property string $created_date
 * @property string $updated_by
 * @property string $updated_date
 *
 * The followings are the available model relations:
 * @property Content $contentCategory
 * @property Content[] $contents
 */
class Content extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Content the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'content';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, desc, content_categoryId', 'required'),
			array('content_categoryId', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>25),
			array('icon', 'length', 'max'=>25),
			array('image', 'length', 'max'=>100),
			array('created_by, updated_by', 'length', 'max'=>50),
			array('created_date, updated_date, desc_content', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, content_categoryId, title, icon, desc, desc_content, image, created_by, created_date, updated_by, updated_date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'contentCategory' => array(self::BELONGS_TO, 'Content', 'content_categoryId'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'content_categoryId' => 'Content Category',
			'title' => 'Title',
			'desc' => 'Desc',
			'desc_content' => 'Desc Content',
			'icon' => 'Icon',
			'image' => 'Image',
			'created_by' => 'Created By',
			'created_date' => 'Created Date',
			'updated_by' => 'Updated By',
			'updated_date' => 'Updated Date',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('content_categoryId',$this->content_categoryId);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('desc',$this->desc,true);
		$criteria->compare('desc_content',$this->desc,true);
		$criteria->compare('icon',$this->desc,true);
		$criteria->compare('image',$this->image,true);
		$criteria->compare('created_by',$this->created_by,true);
		$criteria->compare('created_date',$this->created_date,true);
		$criteria->compare('updated_by',$this->updated_by,true);
		$criteria->compare('updated_date',$this->updated_date,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function behaviors() {
      return array(
          'contentImgBehavior' => array(
              'class' => 'ImageARBehavior',
              'attribute' => 'image', // this must exist
              'extension' => 'png, gif, jpg', // possible extensions, comma separated
              'prefix' => 'cnt_',
              'relativeWebRootFolder' => 'images/contents', // this folder must exist
			  'formats' => array(
				  'thumb' => array(
					'suffix' => '_thumb',
					'process' => array('resize' => array(64, 64)),
				  ),
			   ),
              // 'formats' => array(
                  // 'normal' => array(
                      // 'process' => array('resize' => array(240, 420)),
                  // ),
              // ),
              'defaultName' => 'default', // when no file is associated, this one is used
          ),
      );
   }
}